package com.wanpinghui.supplier.product;

import android.app.Activity;
import android.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.product.adapter.SelectFormatAdapter;
import com.wanpinghui.supplier.product.interfaces.SelectFormatListener;
import com.wanpinghui.supplier.widgets.PlusMinuesView;
import com.wanpinghui.wphlibrary.model.product.bean.ProductBean;
import com.wanpinghui.wphlibrary.model.product.bean.ProductFormatBean;
import com.yunshl.yunshllibrary.utils.NumberUtil;
import com.yunshl.yunshllibrary.view.YunShlPopupWinow;

import org.xutils.common.util.DensityUtil;

import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/21
 ***/

public class FormatManager {
    private Activity context;
    private YunShlPopupWinow popupWinow;
    private SureClick sureClick;
    private List<ProductFormatBean> datas;
    private View view;
    private TextView textViewName;
    private ImageView imageView;
    private TextView textViewPrice;
    private ListView listView;
    private TextView textViewCount;
    private TextView textViewTotal;
    private TextView textViewRightButton;
    private TextView textViewLeftButton;
    private SelectFormatAdapter mAdapter;
    private PlusMinuesView plusMinuesView;
    private TextView textViewSelectTip;
    private int type;
    private ProductBean mBean;
    private boolean seller;

    public void setType(int type) {
        this.type = type;
    }

    public FormatManager(Activity context) {
        this.context = context;
    }

    public FormatManager(Fragment fragment) {
        this.context = fragment.getActivity();
    }

    public void show(View parentView) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.view_dialog_select_format, null);
            textViewName = (TextView) view.findViewById(R.id.tv_name);
            textViewRightButton = (TextView) view.findViewById(R.id.tv_right_button);
            imageView = (ImageView) view.findViewById(R.id.iv_image);
            listView = (ListView) view.findViewById(R.id.listview_format);
            textViewCount = (TextView) view.findViewById(R.id.tv_count);
            textViewTotal = (TextView) view.findViewById(R.id.tv_total);
            textViewPrice = (TextView) view.findViewById(R.id.tv_price);
            plusMinuesView = (PlusMinuesView) view.findViewById(R.id.view_plus_minues);
            textViewLeftButton = (TextView) view.findViewById(R.id.tv_left_button);
            textViewSelectTip = (TextView) view.findViewById(R.id.tv_select_format);
        }
        mAdapter = new SelectFormatAdapter(context);
        listView.setAdapter(mAdapter);
        setDataView();
        mAdapter.setListener(new SelectFormatListener() {
            @Override
            public void onFormatChange(int allCount, double allTotal) {
                textViewCount.setText(Integer.toString(allCount));
                if (allTotal == -1) {
                    textViewTotal.setText("议价");
                } else {
                    textViewTotal.setText("￥" + NumberUtil.double2String(allTotal));
                }
            }
        });
        textViewRightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sureClick != null) {
                    if (type == 1) {
                        sureClick.onCreateOrder(mBean);
                    } else {
                        sureClick.onSure(mBean);
                    }
                }
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
            }
        });
        textViewLeftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sureClick != null) {
                    sureClick.onAddToCart(mBean);
                }
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
            }
        });

        if (type == 1) {
            textViewLeftButton.setVisibility(View.VISIBLE);
            if (seller) {
                textViewRightButton.setText("代他采购");
            } else {
                textViewRightButton.setText("立即采购");
            }
        } else {
            textViewLeftButton.setVisibility(View.GONE);
            textViewRightButton.setText("确认");
        }

        popupWinow = new YunShlPopupWinow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWinow.setCommonConfig(context, YunShlPopupWinow.AnimaDirect.DIRECT_BOTTOM_TOP);
        popupWinow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
    }

    public void setSeller(boolean seller) {
        this.seller = seller;
    }

    private void setDataView() {
        if (view != null && mBean != null) {
            if (mBean.isIs_consult()) {
                textViewPrice.setText("议价");
            } else {
                if (mBean.getMax_price() == mBean.getMin_price() || mBean.getMax_price() < mBean.getMin_price()) {
                    String priceStr = "￥"+NumberUtil.double2String(mBean.getMin_price());
                    SpannableString priceSpanStr = new SpannableString(priceStr);
                    if (priceStr.indexOf(".") >= 0) {
                        priceSpanStr.setSpan(new RelativeSizeSpan(0.77f), priceStr.indexOf("."), priceStr.indexOf(".") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    }
                    textViewPrice.setText(priceSpanStr);
                } else {
                    String priceStr = "￥" + NumberUtil.double2String(mBean.getMin_price()) + "-" + NumberUtil.double2String(mBean.getMax_price());
                    SpannableString priceSpanStr = new SpannableString(priceStr);
                    if (priceStr.indexOf(".") >= 0) {
                        priceSpanStr.setSpan(new RelativeSizeSpan(0.77f), priceStr.indexOf("."), priceStr.indexOf(".") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    }
                    if (priceStr.lastIndexOf(".") != priceStr.indexOf(".")) {
                        priceSpanStr.setSpan(new RelativeSizeSpan(0.77f), priceStr.lastIndexOf("."), priceStr.lastIndexOf(".") + 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    }
                    textViewPrice.setText(priceSpanStr);
                }
            }
            Glide.with(context).load(mBean.getMainImage()).asBitmap().placeholder(R.mipmap.goods_bg_loading_a).error(R.mipmap.goods_bg_loading).into(imageView);
            textViewName.setText(mBean.getName());
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) listView.getLayoutParams();
            if (mBean.getFormatCount() <= 1) {
                listView.setVisibility(View.GONE);
                plusMinuesView.setVisibility(View.VISIBLE);
                if (mBean.getFormatCount() == 1) {
                    plusMinuesView.setCount(datas.get(0).getCount());
                } else {
                    mBean.setSelectCount(mBean.getSelectCount());
                }

                textViewSelectTip.setVisibility(View.GONE);
                plusMinuesView.setOnCountChangeListener(new PlusMinuesView.OnCountChangeListener() {
                    @Override
                    public void onPlus() {
                    }

                    @Override
                    public void onMinues() {
                    }

                    @Override
                    public void onCountChange(int count) {
                        if (mBean.getFormatCount() == 1) {
                            datas.get(0).setCount(count);
                        } else {
                            mBean.setSelectCount(count);
                        }
                        setTotalOneOrNoFormat();
                    }
                });
                setTotalOneOrNoFormat();
            } else {
                listView.setVisibility(View.VISIBLE);
                plusMinuesView.setVisibility(View.GONE);
                textViewSelectTip.setVisibility(View.VISIBLE);
                mAdapter.setDatas(datas);
                if (datas.size() == 2) {
                    params.height = DensityUtil.dip2px(108);
                } else {
                    params.height = DensityUtil.dip2px(164);
                }
                textViewCount.setText(Integer.toString(mAdapter.getAllCount()));
                if (mAdapter.getAllTotal() == -1) {
                    textViewTotal.setText("议价");
                } else {
                    textViewTotal.setText("￥" + NumberUtil.double2String(mAdapter.getAllTotal()));
                }
            }
        }
    }

    private void setTotalOneOrNoFormat() {
        if (mBean.getFormatCount() == 1) {
            textViewCount.setText(Integer.toString(datas.get(0).getCount()));
        } else {
            textViewCount.setText(Integer.toString(mBean.getCount()));
        }

        if (mBean.getFormatCount() == 0) {
            if (mBean.isIs_consult()) {
                textViewTotal.setText("议价");
            } else {
                textViewTotal.setText("￥" + NumberUtil.double2String(mBean.getCount() * mBean.getPrice()));
            }
        } else {
            if (mBean.getFormatList().get(0).isIs_consult()) {
                textViewTotal.setText("议价");
            } else {
                textViewTotal.setText("￥" + NumberUtil.double2String(mBean.getFormatList().get(0).getCount() * mBean.getFormatList().get(0).getPrice()));
            }
        }
    }

    public void setDatas(ProductBean productBean) {
        mBean = productBean;
        this.datas = productBean.getFormatList();
        if (mAdapter != null) {
            mAdapter.setDatas(datas);
            setDataView();
        }
    }

    public void setSureClick(SureClick sureClick) {
        this.sureClick = sureClick;
    }

    public interface SureClick {
        void onAddToCart(ProductBean bean);

        void onCreateOrder(ProductBean bean);

        void onSure(ProductBean bean);
    }

}
